#include "bottomdockwindow.h"

extern void setButtonStyle(QPushButton *pushButton, const QString &imageUrl1, const QString &imageUrl2);

BottomDockWindow::BottomDockWindow(QWidget *parent, const Qt::AlignmentFlag &v_alignment, const Qt::AlignmentFlag &h_alignment, const QPoint &pos)
    : StackWindow(parent, v_alignment, h_alignment, pos)
{
    //----------initalize widget
    widget=new QWidget(this);

    widget->setFixedSize(parent->width(),100);
    this->setFixedSize(parent->width()+bottomSideButtonWidth,150);
    this->move(this->x(),this->y()-bottomSideButtonHeight);

    mouseFilter=new MouseFilter(this);

    this->installEventFilter(mouseFilter);


    bottomSideButton=new QPushButton(this);
    bottomSideButton->setFixedSize(bottomSideButtonWidth,bottomSideButtonHeight);
    bottomSideButton->move(widget->width()/2-bottomSideButtonWidth/2,widget->height());
    isShow = false;
    // 设置按钮样式跟随侧边栏状态而变化
    changeBottomButtonStyle(false);
    //直接移动侧边栏无效，可能是界面重绘不支持。所以间隔0.2秒后再移动
    QTimer::singleShot(200, this, SLOT(slotMoveWidget()));
    //初始化动画
    anim1 = new QPropertyAnimation(widget, "geometry");
    anim1->setEasingCurve(QEasingCurve::Linear);
    anim1->setDuration(500);
    anim2 = new QPropertyAnimation(bottomSideButton, "geometry");
    anim2->setEasingCurve(QEasingCurve::Linear);
    anim2->setDuration(500);
    widget->move(0,150);
    connect(bottomSideButton,&QPushButton::clicked,this,&BottomDockWindow::bottomSideButtonClicked);


    QPushButton *q1 = new QPushButton(this->widget);
    QPushButton *q2 = new QPushButton(this->widget);
    QPushButton *q3 = new QPushButton(this->widget);
    QPushButton *q4 = new QPushButton(this->widget);
    QPushButton *q5 = new QPushButton(this->widget);



    QString *imageurl1=new QString(":/image/hangpai_5.svg");
    QString *imageurl2=new QString(":/image/hangpai_2.svg");

    QString *imageurl3=new QString(":/image/hangpai_3.svg");
    QString *imageurl4=new QString(":/image/hangpai_1.svg");

    QString *imageurl5=new QString(":/image/hangpai_4.svg");
    QString *imageurl6=new QString(":/image/hangpai.svg");

    QString *imageurl7=new QString(":/image/diqiu_1.svg");
    QString *imageurl8=new QString(":/image/diqiu.svg");

    QString *imageurl9=new QString(":/image/box.svg");
    QString *imageurl10=new QString(":/image/box_o.svg");


    QSize size=QSize(80,80);
    q1->setFixedSize(size);
    q2->setFixedSize(size);
    q3->setFixedSize(size);
    q4->setFixedSize(size);
    q5->setFixedSize(size);
    setButtonStyle(q1,*imageurl1,*imageurl2);
    setButtonStyle(q2,*imageurl3,*imageurl4);
    setButtonStyle(q3,*imageurl5,*imageurl6);
    setButtonStyle(q4,*imageurl7,*imageurl8);
    setButtonStyle(q5,*imageurl9,*imageurl10);

    QHBoxLayout *layout = new QHBoxLayout(widget);
    layout->addStretch();
    layout->addWidget(q1);
    layout->addStretch();
    layout->addWidget(q2);
    layout->addStretch();
    layout->addWidget(q3);
    layout->addStretch();
    layout->addWidget(q4);
    layout->addStretch();
    layout->addWidget(q5);
    layout->addStretch();

    //set the layout alignment
    layout->setAlignment(_v_alignmentFlag | _h_alignmentFlag);
    widget->setVisible(true);
    widget->setLayout(layout);
    //widget->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    widget->setStyleSheet("background-color: rgba(211, 211, 211, 0.3);");//set background color
    connect(q1, &QPushButton::clicked, [=](){
        emit startPlot();
    });
}
/* 设置按钮样式跟随侧边栏状态而变化 */
void BottomDockWindow::changeBottomButtonStyle(bool isShowSide)
{
    QString qss;
    if(isShowSide) {
        qss = "QPushButton {border-image: url(:/image/down-square-o.png);border-radius: 6px;}"
                "QPushButton:hover {border-image: url(:/image/down-square-o.png);}"
                "QPushButton:pressed {border-image: url(:/image/down-square.png);}";
    } else {
        qss = "QPushButton {border-image: url(:/image/up-square-o.png);border-radius: 6px;}"
                "QPushButton:hover {border-image: url(:/image/up-square-o.png);}"
                "QPushButton:pressed {border-image: url(:/image/up-square.png);}";
    }
    this->bottomSideButton->setStyleSheet(qss);
}
void BottomDockWindow::bottomSideButtonClicked()
{
    // 侧边栏和按钮动画
    if(isShow) {
        isShow = false;
        anim1->setStartValue(QRect(0, 50, widget->width(), widget->height()));
        anim1->setEndValue(QRect(0, widget->height()+50, widget->width(), widget->height()));

        anim2->setStartValue(QRect(widget->width()/2-25, 0, widget->width(), widget->height()));
        anim2->setEndValue(QRect(widget->width()/2-25, 100, widget->width(), widget->height()));
    } else {
        isShow = true;
        anim1->setStartValue(QRect(0, widget->height()+50, widget->width(), widget->height()));
        anim1->setEndValue(QRect(0, 50, widget->width(), widget->height()));

        anim2->setStartValue(QRect(widget->width()/2-25, 100, widget->width(), widget->height()));
        anim2->setEndValue(QRect(widget->width()/2-25, 0, widget->width(), widget->height()));
    }
    anim1->start();
    anim2->start();

    changeBottomButtonStyle(isShow);
}

void BottomDockWindow::slotMoveWidget()
{
    widget->move(widget->width(), 0);
}

